package com.cn.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cn.entity.Message;
import com.cn.entity.User;
import com.cn.mapper.user.MessageMapper;
import com.cn.mapper.manager.ManagerMapper;
import com.cn.mapper.user.UserMapper;
import com.cn.service.IUserService;
import com.cn.vo.userVo.Friend;
import com.cn.vo.userVo.Request;
import com.rabbitmq.tools.json.JSONUtil;
import io.lettuce.core.RedisCommandExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName UserService
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/11/7 8:56
 * @Version 1.0
 */
@Service("UserServiceImp")
public class UserServiceImp implements IUserService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    User user;
    @Autowired
    ManagerMapper managerMapper;
    @Autowired
    Message message;

    /**
     * 登录服务
     */

    public boolean login(String UserName,String UserPassword){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",UserName);
        queryWrapper.eq("user_password",UserPassword);
        if(userMapper.selectList(queryWrapper).size()>0){
            return true;
        }else {
            return  false;
        }
    }

    @Override
    public Boolean isSuspended(String userName) {
       if(userMapper.isSuspended(userName).size()!=0){
           return true;
       }else {
           return  false;
       }
    }

    @Override
    public void cancelSuspended(String userName) {
        userMapper.cancelSuspended(userName);
    }

    @Override
    public void suspendUser(String userName) {
        userMapper.accountSuspended(userName);
    }


    @Override
    public void deleteUser(String admin) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",admin);
        userMapper.delete(queryWrapper);
    }

    /**
     * 注册服务

     */
    public void register(User user){
        String key="user:"+user.getUserName();
        redisTemplate.opsForValue().set(key,user);
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        String key="user:"+user.getUserName();
        userMapper.updateUser(user);

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_name",user.getUserName());
        redisTemplate.opsForValue().set(key,userMapper.selectList(queryWrapper));

    }
    @Override
    public Object showUserById(String userName) {


        String key="user:"+userName;
//        先查缓存
//        userList.add(redisTemplate.opsForValue().get(key));

       try{
           if(redisTemplate.opsForValue().get(key)==null){ //判断Redis数据库是否存在该数据
               QueryWrapper queryWrap=new QueryWrapper();
               queryWrap.eq("user_name",userName);
//            加入缓存
               System.out.println("——————————————————》加入Redis缓存成功《————————————————");
               redisTemplate.opsForValue().set(key,userMapper.selectList(queryWrap));
               System.out.println("——————————————————》查SQL《————————————————");
               return userMapper.selectList(queryWrap);


           }else {//不存在，从MySQL数据库中查询
               System.out.println("——————————————————》查Redis缓存《————————————————");
               return redisTemplate.opsForValue().get(key);
           }
       }catch(Exception e){
           System.out.println("连接错误");
           QueryWrapper queryWrap=new QueryWrapper();
           queryWrap.eq("user_name",userName);
           return userMapper.selectList(queryWrap);
       }


    }
    @Override
    public List<Friend> showFriendById(String userName) {
        return userMapper.showFriend(userName);
    }

    @Override
    public void deleteFriendByAdmin(String userName, String friendUsername) {
            userMapper.deleteFriendByAdmin(userName,friendUsername);
            userMapper.deleteFriendByAdmin2(userName,friendUsername);
    }




    @Override
    public List<Request> showRequest(String receiveAdmin) {
        return userMapper.showRequest(receiveAdmin);
    }

    @Override
    public void sendRequest(String reqAdmin, String reqName,String recAdmin, String recName) {
        userMapper.sendRequest(reqAdmin,reqName,recAdmin,recName);
    }

    /**
     * 发出好友申请
     * @Param:UserId
     */
    @Override
    public void sendBeFriendRequest(int request_id,int receive_id) {
            userMapper.sendBeFriendRequest(request_id,receive_id);
    }
    /**
     * 成功添加好友
     */
    @Override
    public void BeFriend(String recAdmin,String recName,String reqAdmin,String reqName) {
        userMapper.beFriendForRequest(recAdmin,recName,reqAdmin,reqName);
        userMapper.beFriendForReceive(recAdmin,recName,reqAdmin,reqName);
        userMapper.deleteRequst(recAdmin,reqAdmin);
//        userMapper.deleteRequst(recAdmin,reqAdmin);

    }

    @Override
    public void refuseBeFriend(String recAdmin, String reqAdmin) {
        userMapper.deleteRequst(recAdmin,reqAdmin);
    }

    /**
     * 发送私信
     * @param message
     */
    @Override
    public void sendMassageForFriendById(Message message) {
        message.setMessageId(IdWorker.getId());
        messageMapper.insert(message);
    }

    @Override
    public List<Message> showMassage(String receiver_admin) {
        QueryWrapper queryWrap=new QueryWrapper();
        queryWrap.eq("receiver_Admin",receiver_admin);
        return messageMapper.selectList(queryWrap);
    }

    @Override
    public void warningMessageForSystem(String receiver_admin) {
       message.setReceiverAdmin(receiver_admin);
       message.setSenderName("System");
       message.setSenderAdmin("admin");
        messageMapper.insert(message);
    }

    @Override
    public List<Message> flexibleQuery(String message) {
        return userMapper.flexibleQueryMessage(message);
    }

    @Override
    public List<User> flexibleUser(String query) {
     return    userMapper.flexibleQueryUser(query);
    }

    @Override
    public List<Message> showMassageBySender(String sender_admin, String receiver_admin) {
        QueryWrapper queryWrap=new QueryWrapper();
        queryWrap.eq("receiver_Admin",receiver_admin);
        queryWrap.eq("sender_admin",sender_admin);
        return messageMapper.selectList(queryWrap);
    }


    @Override
    public void hasBeenRead(int receiver_id) {
        UpdateWrapper update = new UpdateWrapper();
        update.set("is_read",1);
        update.eq("receiver_admin",receiver_id);
        messageMapper.update(null,update);
    }

    @Override
    public boolean ManageLogin(String ManagerAdmin, String ManagerPassword) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("manager_admin",ManagerAdmin);
        queryWrapper.eq("manager_password",ManagerPassword);
        if(managerMapper.selectList(queryWrapper).size()!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<User> showAll() {
        return userMapper.selectList(null);
    }

    @Override
    public List<Message> showAllMessage() {
        return messageMapper.selectList(null);
    }
}
